#!/usr/bin/python
# coding=utf-8
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError


class object_detection:
    def __init__(self):
        self.bridge = CvBridge()
        self.detection_pub = rospy.Publisher(
            "/usb_cam/image_raw/detection", Image)
        self.image_sub = rospy.Subscriber(
            "/usb_cam/image_raw", Image, self.callback)

    def callback(self, data):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError as e:
            print(e)

        (rows, cols, channels) = cv_image.shape
        if cols > 60 and rows > 60:
            cv2.circle(cv_image, (50, 50), 10, 255)
        rospy.sleep(3.)

        cv2.imshow("Image window", cv_image)
        cv2.waitKey(3)

        try:
            self.detection_pub.publish(
                self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
        except CvBridgeError as e:
            print(e)


if __name__ == '__main__':
    try:
        rospy.init_node('object_detection_node', anonymous=True)
        od = object_detection()
        rospy.spin()
    except KeyboardInterrupt:
        od.serial.close
        rospy.loginfo("object_detection_node shutting down")
